package com.atguigu.dga.governance.assessor.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: TableNoAccessAssessor
 * Package: com.atguigu.dga.governance.assessor.calc
 * Description:
 *
 * @Author:
 * @Create: 2024/3/18 12:53
 * @Version: 1.0
 */
@Component("TABLE_NO_ACCESS")
public class TableNoAccessAssessor extends Assessor {
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        // 指标参数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        Integer paramDays = paramJsonObj.getInteger("days");

        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime(); // 精确到秒的
        Date tableLastAccessYmd = DateUtils.truncate(tableLastAccessTime, Calendar.DAY_OF_MONTH);// 精确到天

        String assessDateStr = assessParam.getAssessDate();
        Date assessYmd = DateUtils.parseDate(assessDateStr, "yyyy-MM-dd");

        long diffMs = assessYmd.getTime() - tableLastAccessYmd.getTime();

        // 差异转成天
        long diffDays = TimeUnit.DAYS.convert(diffMs, TimeUnit.MILLISECONDS);

        if(diffDays >= paramDays) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("超过" + paramDays + "天没有访问");
            governanceAssessDetail.setAssessComment("实际超过" + diffDays + "天没有访问");
        }


    }
}
